package com.cykj.web.controller.posbiz.entry.controller;

import java.util.List;
import java.util.Arrays;

import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.common.utils.SecurityUtils;
import com.cykj.pos.mapper.BizPosMachineMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cykj.common.annotation.Log;
import com.cykj.common.core.controller.BaseController;
import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.enums.BusinessType;
import com.cykj.web.controller.posbiz.entry.domain.BizTopupBills;
import com.cykj.web.controller.posbiz.entry.service.IBizTopupBillsService;
import com.cykj.common.utils.poi.ExcelUtil;
import com.cykj.common.core.page.TableDataInfo;

/**
 * 充值记录Controller
 *
 * @author zhj
 * @date 2021-11-11
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/bills" )
public class BizTopupBillsController extends BaseController {

    private final IBizTopupBillsService iBizTopupBillsService;
    @Autowired
    BizPosMachineMapper posMachineMapper;
    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('pos:bills:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizTopupBills bizTopupBills) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long merchId = posMachineMapper.selectMerchId(sysUser.getUserId());
        bizTopupBills.setBillMerchId(merchId.toString());
        startPage();
        List<BizTopupBills> list = iBizTopupBillsService.queryLists(bizTopupBills);
        return getDataTable(list);
    }

    /**
     * 导出充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('pos:bills:export')" )
    @Log(title = "充值记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BizTopupBills bizTopupBills) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long merchId = posMachineMapper.selectMerchId(sysUser.getUserId());
        bizTopupBills.setBillMerchId(merchId.toString());
        List<BizTopupBills> list = iBizTopupBillsService.queryLists(bizTopupBills);
        ExcelUtil<BizTopupBills> util = new ExcelUtil<BizTopupBills>(BizTopupBills.class);
        return util.exportExcel(list, "bills" );
    }

    /**
     * 获取充值记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('pos:bills:query')" )
    @GetMapping(value = "/{billId}" )
    public AjaxResult getInfo(@PathVariable("billId" ) Long billId) {
        return AjaxResult.success(iBizTopupBillsService.checkById(billId));
    }
    /*删除充值记录*/
    @DeleteMapping("/{billId}" )
    public AjaxResult remove(@PathVariable Long billId) {
        return toAjax(iBizTopupBillsService.removes(billId));
    }
    /**
     * 修改充值记录
     */
    @PreAuthorize("@ss.hasPermi('pos:bills:edit')" )
    @Log(title = "充值记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTopupBills bizTopupBills) {
        return toAjax(iBizTopupBillsService.updateById(bizTopupBills) ? 1 : 0);
    }
}
